package u.can.i.up.db.mongodb;

import java.util.List;
import java.util.Map;

import org.bson.Document;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;

import u.can.i.up.utils.db.mongo.MongodbConnFactory;

public class AppInfoByPlayDrone {

	@SuppressWarnings("unchecked")
	public static void readInfo(Map<String, Long> packageDownloadNumberMap, Map<String, String> packageCategoryMap) {
		java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(java.util.logging.Level.SEVERE);
		MongoCollection<Document> mcd = MongodbConnFactory.getCollection("test_metadatas");
		FindIterable<Document> documents = mcd.find();
		for (Document document : documents) {
			String app_name = (String) document.get("apk");

			String category = ((List<String>) ((Document) document.get("app_details")).get("app_category")).get(0);
			packageCategoryMap.put(app_name, category);

			String downloadStr = (String) ((Document) document.get("app_details")).get("num_downloads");
			downloadStr = (downloadStr.contains("+")) ? (downloadStr.replace("+", "").trim()) : (downloadStr.trim());
			downloadStr = (downloadStr.contains("-")) ? (downloadStr.replace("-", "").trim()) : (downloadStr.trim());
			downloadStr = (downloadStr.contains(",")) ? (downloadStr.replace(",", "").trim()) : (downloadStr.trim());
			long downloadNums = Long.parseLong(downloadStr);
			if (packageDownloadNumberMap.containsKey(app_name)) {
				packageDownloadNumberMap.put(app_name, packageDownloadNumberMap.get(app_name) + downloadNums);
			} else {
				packageDownloadNumberMap.put(app_name, downloadNums);
			}
		}
	}
}
